package com.jinghua.treatment.service.impl;

import com.jinghua.treatment.common.Pager;
import com.jinghua.treatment.entity.SysDoctor;
import com.jinghua.treatment.entity.SysImMessages;
import com.jinghua.treatment.entity.SysImMessagesExample;
import com.jinghua.treatment.mapper.SysDoctorMapper;
import com.jinghua.treatment.mapper.SysImMessagesMapper;
import com.jinghua.treatment.mapper.SysPatientMapper;
import com.jinghua.treatment.service.IMMessageService;
import io.swagger.models.auth.In;
import org.springframework.core.convert.converter.ConvertingComparator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class IMMessageServiceImpl implements IMMessageService {
    @Resource
    private SysImMessagesMapper sysImMessagesMapper;
    @Resource
    private SysDoctorMapper sysDoctorMapper;
    @Resource
    private SysPatientMapper sysPatientMapper;

    @Override
    public void save(SysImMessages sysImMessages) {
        sysImMessagesMapper.insertSelective(sysImMessages);
    }

    @Override
    public void loadMyMessages(String whos, Integer fromId, Integer toId, Pager pager) {
        pager.setPageSize(30);//加载30条
        SysImMessagesExample sysImMessagesExample = new SysImMessagesExample();
        sysImMessagesExample.setLimitStart(pager.getStart());
        sysImMessagesExample.setLimitEnd(pager.getPageSize());
        sysImMessagesExample.setOrderByClause(" send_time  desc");

        SysImMessagesExample.Criteria sysImMessagesCriteria = sysImMessagesExample.createCriteria();
        sysImMessagesCriteria.andFromIdEqualTo(fromId);
        sysImMessagesCriteria.andToIdEqualTo(toId);

        SysImMessagesExample.Criteria sysImMessagesCriteria2 = sysImMessagesExample.createCriteria();
        sysImMessagesCriteria2.andFromIdEqualTo(toId);
        sysImMessagesCriteria2.andToIdEqualTo(fromId);

        sysImMessagesExample.or(sysImMessagesCriteria2);
        /*if("doctor".equals(whos)){
            SysDoctor sysDoctor = sysDoctorMapper.selectByPrimaryKey(Integer.valueOf(id));
            sysImMessagesExample
            sysImMessagesMapper.selectByExample();
        }*/
        List<SysImMessages> sysImMessageList = sysImMessagesMapper.selectByExample(sysImMessagesExample);
        //手动再次排序
        Collections.sort(sysImMessageList,new Comparator<SysImMessages>(){
            @Override
            public int compare(SysImMessages o1, SysImMessages o2) {
                return o1.getSendTime().after(o2.getSendTime())?1:-1;
            }
        });
        pager.setRows(sysImMessageList);
    }
}
